package com.cby.feedback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cby.feedback.client.AlgorithmClient;
import com.cby.feedback.entity.CbyFeedback;
import com.cby.feedback.entity.query.CbyFeedBackQuery;
import com.cby.feedback.entity.vo.CbyFeedBackVo;
import com.cby.feedback.mapper.CbyFeedbackMapper;
import com.cby.feedback.service.CbyFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cby.other.R;
import com.dam.common.Result;
import com.dam.entity.Algorithm;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wskh
 * @since 2021-11-07
 */
@Service
public class CbyFeedbackServiceImpl extends ServiceImpl<CbyFeedbackMapper, CbyFeedback> implements CbyFeedbackService {

    @Autowired
    CbyFeedbackService feedbackService;
    @Autowired
    AlgorithmClient algorithmClient;

    @Override
    public R queryFeedBack(long current, long limit, CbyFeedBackQuery cbyFeedBackQuery) {
        // 创建page对象
        Page<CbyFeedback> feedbackPage = new Page<>(current,limit);
        // 构建条件
        QueryWrapper<CbyFeedback> queryWrapper = new QueryWrapper<>();
//        String name = cbyUserQuery.getName();

        // 按照添加时间排序
        queryWrapper.orderByDesc("insert_time");
//        if (!StringUtils.isEmpty(name)) {
//            queryWrapper.like("name", name);
//        }
        // 调用方法实现 分页条件查询
        IPage<CbyFeedback> page = feedbackService.page(feedbackPage, queryWrapper);
        List<CbyFeedback> records = page.getRecords();

        List<CbyFeedBackVo> feedBackVoList = new ArrayList<>();
        for (CbyFeedback feedback : records) {
            CbyFeedBackVo feedBackVo = new CbyFeedBackVo();
            BeanUtils.copyProperties(feedback,feedBackVo);
            Result result = algorithmClient.getAlgorithm(feedBackVo.getAlgorithmId());
            Algorithm algorithm = (Algorithm)result.getData().get("algorithm");
            feedBackVo.setAlgorithmName(algorithm.getName());
            feedBackVoList.add(feedBackVo);
        }
        return R.ok().data("total",page.getTotal()).data("rows",feedBackVoList);
    }
}
